<template>
  <div class="execute">
    <div id="gen-pdf" v-loading.fullscreen.lock="genPdf">
      <c-section title="门店建设基本信息">
        <template v-slot:right>
          <c-abnormal-preview gen-pdf="gen-pdf-hide" path="/storeConstruction/exceptionManage" :query="{ constCode: detail.constCode, functionModel: 'SHOP_DECORATION' }" />
        </template>
        <c-list ref="form" :model="formData" :rules="formRules" :column="3" label-width="130px">
          <c-list-item label="门店编码" :value="detail.shopCode" />
          <c-list-item label="门店" :value="detail.shopName" />
          <c-list-item label="所属客户编码" :value="detail.clientCode" />
          <c-list-item label="所属客户" :value="detail.clientName" />
          <c-list-item label="所属办事处" :value="detail.officeName" />
          <c-list-item label="门店类型" dict-type-code="SHOP_TYPE" :value="detail.shopType" />
          <c-list-item label="门店业务类型" dict-type-code="BIZ_TYPE" :value="detail.shopBizType" />
          <c-list-item label="门店城市级别" dict-type-code="CITY_LEVEL" :value="detail.shopCityLevel" />
          <c-list-item label="门店详细地址" :value="detail.shopAddress" :span="3" />
          <c-list-item label="门店经营面积" suffix="m²" :value="detail.shopArea || 0" />
          <c-list-item label="门店建设项目号" :value="detail.constCode" />
          <c-list-item label="报图类型" dict-type-code="MARKET_TYPE" :value="detail.marketType" />
          <c-list-item label="门店建设类型" dict-type-code="CONST_TYPE" :value="detail.constType" />
          <c-list-item label="商家类别" dict-type-code="BIZMAN_TYPE" :value="detail.bizmanType" />
          <c-list-item label="装修形式" dict-type-code="DECORATION_FORM" :value="detail.decorationFormCal || detail.decorationForm" />
          <c-list-item label="验收类型" dict-type-code="ACCEPT_TYPE" :value="detail.acceptType" />
          <c-list-item style="color:#3DC094;cursor: pointer;" label="对应硬装验收任务号" :value="detail.upAcceptTaskCode" @click.native="toHardPage(detail.upAcceptTaskCode)" />
          <c-list-item label="形象督导" :value="detail.formSupervisorName" />
          <c-list-item label="验收项目专员" :value="detail.accepterName" />
          <c-list-item v-if="!genPdf" label="实际验收时间" prop="actualAcceptTime">
            <el-date-picker
              v-model="formData.actualAcceptTime"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
              clearable
            />
          </c-list-item>
          <c-list-item v-else label="实际验收时间" :value="formData.actualAcceptTime" />
          <c-list-item v-if="!genPdf" label="验收卖场总面积" prop="acceptShopTotalArea">
            <c-input-number v-model="formData.acceptShopTotalArea" float placeholder="验收卖场总面积" clearable />
          </c-list-item>
          <c-list-item v-else label="验收卖场总面积" suffix="m²" :value="formData.acceptShopTotalArea || 0" />

          <c-list-item :span="3" label="备注" prop="remark">
            <el-input v-model="formData.remark" maxlength="1000" show-word-limit type="textarea" placeholder="请填写备注" />
          </c-list-item>
          <c-list-item :span="3" label="异常申请确认" prop="abnormalConfirmation">
            <el-input v-model="formData.abnormalConfirmation" maxlength="1000" show-word-limit type="textarea" placeholder="请填写异常申请确认" />
          </c-list-item>
          <c-list-item label="审核退回意见" :value="detail.lastReturnOpinion" :span="3" />

        </c-list>
      </c-section>
      <c-section title="门店验收问题明细">
        <ProblemDetail ref="soft" acc-type="soft" type="soft" show-btn @save="printSave" @gen-pdf="genPdf=$event" />
      </c-section>
    </div>
    <c-section title="软装验收附件">
      <c-file-table-box v-model="files" belong-model="SOFT_REPORT" :model-id="$route.query.id" />
    </c-section>
    <div class="btn-container">
      <c-button @click="clickCancelHandle">取消</c-button>
      <c-button type="primary" @click="clickConfirmHandle">保存</c-button>
      <c-button type="primary" @click="clickConfirmHandle('commit')">确定</c-button>
    </div>
  </div>
</template>
<script>
import ProblemDetail from '../components/problemDetail'
import { getSoftDetail, postCommitSoftDetail } from '@/api/check'
export default {
  name: 'CheckSoftExecute',
  components: {
    ProblemDetail
  },
  data() {
    return {
      genPdf: false,
      detail: {},
      formData: {
        actualAcceptTime: '',
        acceptShopTotalArea: '',
        remark: '',
        abnormalConfirmation: ''
      },
      formRules: {
        actualAcceptTime: [{ required: true, message: '请选择实际验收时间', trigger: 'change' }],
        acceptShopTotalArea: [{ required: true, message: '请填写验收卖场总面积', trigger: 'change' }]
      },
      files: [],
      selectionFiles: []
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    toHardPage(param) {
      if (param) {
        // 打开新页签
        this.$router.push({ path: '/check/hard/detail', query: { taskCode: param, id: this.detail.hardId, unitCode: this.detail.constUnitCode }})
      }
    },
    getDetail() {
      const { id } = this.$route.query
      getSoftDetail(id).then(res => {
        this.detail = res.data
        this.$set(this.formData, 'actualAcceptTime', this.detail.actualAcceptTime)
        this.$set(this.formData, 'acceptShopTotalArea', this.detail.acceptShopTotalArea)
        this.$set(this.formData, 'remark', this.detail.remark)
        this.$set(this.formData, 'abnormalConfirmation', this.detail.abnormalConfirmation)
      })
    },
    // 打印之前的保存
    printSave(type) {
      return this.$refs.form.validate().then(() => {
        return this.$refs.soft.genData().then(softAcceptDutyDTOList => {
          const { id } = this.$route.query
          const data = {
            id,
            ...this.formData,
            softAcceptDutyDTOList: softAcceptDutyDTOList || [],
            acceptTaskCode: this.detail.acceptTaskCode,
            upAcceptTaskCode: this.detail.upAcceptTaskCode
          }
          return postCommitSoftDetail(data, 'save').then(res => {
            const routeUrl = this.$router.resolve({
              path: '/printReport',
              query: { id: this.$route.query.id, type }
            })
            window.open(routeUrl.href, '_blank')

            // this.$message.success('保存成功！')
          })
        })
      })
    },
    clickConfirmHandle(type = 'save') {
      return this.$refs.form.validate().then(() => {
        return this.$refs.soft.genData().then(softAcceptDutyDTOList => {
          const files = this.$getFiles(this.files, id)
          const _files = files.filter(e => e.dr !== '1' && e.dr !== 1)
          if (_files.length === 0) {
            this.$message.warning('至少上传一个附件！')
            return Promise.reject('')
          }
          for (let i = 0; i < _files.length; i++) {
            const v = _files[i]
            if (!v.attachmentName) {
              this.$message.warning(`请输入第${i + 1}行附件名称！`)
              return Promise.reject('')
            }
            if (!v.configAttachmentId) {
              this.$message.warning(`请选择第${i + 1}行附件类型！`)
              return Promise.reject('')
            }
          }
          const { id } = this.$route.query
          const data = {
            id,
            ...this.formData,
            softAcceptDutyDTOList: softAcceptDutyDTOList || [],
            files,
            acceptTaskCode: this.detail.acceptTaskCode,
            upAcceptTaskCode: this.detail.upAcceptTaskCode
          }
          return postCommitSoftDetail(data, type).then(res => {
            this.$message.success('保存成功！')
            this.$store.dispatch('views/goFirst')
          })
        })
      })
    },
    clickCancelHandle() {
      this.$store.dispatch('views/goFirst')
    }
  }
}
</script>
<style lang="scss" scoped>
.execute{

}
</style>
